﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using BLL;
using Common;
using System.Data.SqlClient;
using Model;
using System.Text.RegularExpressions;

namespace WebPortal.Controllers
{
    public class SysViewPushController : ViewInfo
    {
        /// <summary>
        /// 推送信息列表
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult List(string dbName, int rows = 10, int page = 1)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            IDataParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@ViewGroup", "");
            paras[1] = new SqlParameter("@ViewName", "");
            paras[2] = new SqlParameter("@ViewType", "");
            paras[3] = new SqlParameter("@PushType", "");
            paras[4] = new SqlParameter("@Rows", rows);
            paras[5] = new SqlParameter("@PageNum", page);
            paras[6] = new SqlParameter("@SysCode", loginInfos.ETLTargetCode);
            List<SysViewPush> list = bll.GetPushList(paras);
            int count = bll.GetPushCount(paras);
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);

            return Json(jsonDict);
        }
        public ActionResult GetList(string dbName, string g, string n, string t, string p, int rows = 10, int page = 1)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            IDataParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@ViewGroup", g);
            paras[1] = new SqlParameter("@ViewName", n);
            paras[2] = new SqlParameter("@ViewType", t);
            paras[3] = new SqlParameter("@PushType", p);
            paras[4] = new SqlParameter("@Rows", rows);
            paras[5] = new SqlParameter("@PageNum", page);
            paras[6] = new SqlParameter("@SysCode", Request.Cookies["MDM_TargetCode"].Value);
            List<SysViewPush> list = bll.GetPushList(paras);
            int count = bll.GetPushCount(paras);
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);

            return Json(jsonDict);
        }
        /// <summary>
        /// 推送信息项
        /// </summary>
        /// <param name="viewGroup">视图组名</param>
        /// <returns></returns>
        public string PushConfig(string dbName, string viewGroup)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            IDataParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@ViewGroup", viewGroup);
            DataTable dt = bll.PushConfig(paras);
            System.Text.StringBuilder html = new System.Text.StringBuilder();
            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                string vt = dr["ViewType"].ToString();
                switch (vt)
                {
                    case "0":
                        vt = "基础视图";
                        break;
                    case "1":
                        vt = "自定义视图";
                        break;
                    case "2":
                        vt = "上传视图";
                        break;
                }
                //判断选项选择状态
                string ssis = "";
                if (dr["IsSSISPush"].ToString() == "1")
                    ssis = "checked='checked'";
                string ws = ""; ;
                if (dr["IsWebServicePush"].ToString() == "1")
                    ws = "checked='checked'";
                string full = "";
                if (dr["IsFullAmount"].ToString() == "1")
                    full = "checked='checked'";
                string dis = "";
                if (dr["IsDisable"].ToString() == "1")
                    dis = "checked='checked'";
                string delt = "";
                if (dr["DeletedType"].ToString() == "1")
                    delt = "checked='checked'";
                string ibd = "";
                if (dr["IsBackDelete"].ToString() == "1")
                    ibd = "checked='checked'";
                html.Append(string.Format("<tr><td width=\"100\">{0}</td><td width=\"100\">{1}</td><td width=\"60\">{2}</td><td width=\"60\"><input type=\"hidden\" name='ViewID' value='{3}'/><input type=\"hidden\" name='ViewGroupID' value='{4}'/><input type=\"checkbox\" name='IsSSISPush' value='1' {5}/></td><td width=\"100\"><input type=\"checkbox\" name='IsWebServicePush' value='1' {6}/></td><td width=\"60\"><input type=\"checkbox\" name='IsFullAmount' value='1' {7}/></td><td width=\"80\"><input type=\"checkbox\" name='IsDisable' value='1' {8}/></td><td width=\"100\"><input type=\"checkbox\" name='DeletedType' value='1' {9}/></td><td width=\"100\"><input type=\"checkbox\" name='IsBackDelete' value='1' {10}/></td></tr>",
                    dr["GroupName"], dr["ViewName"], vt, dr["ViewID"], viewGroup, ssis, ws, full, dis, delt, ibd));
                i++;
            }
            return html.ToString();
        }
        /// <summary>
        /// 保存推送信息项
        /// </summary>
        /// <param name="collection">提交页面数据</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SavePush(FormCollection collection,string dbName)
        {
            string query = collection["query"].ToString();
            List<Dictionary<string, string>> list_query = new List<Dictionary<string, string>>();
            string[] arr_query;
            if (query.StartsWith("["))
            {
                arr_query = Regex.Split(query.Replace("[[", "").Replace("]]", ""), "],[[]");

                foreach (string q in arr_query)
                {
                    Dictionary<string, string> dict = q.FromJSon<Dictionary<string, string>>();
                    list_query.Add(dict);
                }
            }
            else
            {
                Dictionary<string, string> dict = query.FromJSon<Dictionary<string, string>>();
                list_query.Add(dict);
            }

            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            try
            {
                SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
                foreach (Dictionary<string, string> dic_query in list_query)
                {
                    IDataParameter[] queryParas = new SqlParameter[dic_query.Count];
                    int index = 0;
                    foreach (string item in dic_query.Keys)
                    {
                        queryParas[index] = new SqlParameter("@" + item, dic_query[item]);
                        index += 1;
                    }
                    bll.Update(queryParas, "UpdatePush");

                    new SysOperationLog(loginInfos).Log("视图推送管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.成功, "更新视图推送信息操作,操作成功", loginInfos.UserName);
                }
                return Json("操作成功！");
            }
            catch (Exception ex)
            {
                new SysOperationLog(loginInfos).Log("视图推送管理", Request.UserHostAddress, OperationAction.更新, OperationStatus.失败, "更新视图推送信息操作,操作失败：" + ex.Message, loginInfos.UserName);
                return Json(ex.Message);
            }
        }

        public string DeleteItem(string dbName, string id)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            string msg = bll.DeletePush(id);
            return msg;
        }
    }
}
